package abel.wlmr.model.unit;

import java.util.ArrayList;
import java.util.List;

import abel.wlmr.model.WlmrBasePart;
import abel.wlmr.model.WlmrBaseUnit;
import abel.wlmr.model.WlmrDefaultUnit;
import abel.wlmr.model.WlmrException;
import abel.wlmr.model.type.DataIdBody;
import io.netty.buffer.ByteBuf;

public class AfnSecondPart extends WlmrBasePart {

    public static final short F_CURVE = 401;
    public static final short F_FROZEN_DAY = 409;
    public static final short F_FROZEN_MONTH = 410;

    public AfnSecondPart() {
        super();
    }

    public AfnSecondPart(short guessedLen) {
        super(guessedLen);
    }

    @Override
    public int parseBuf(ByteBuf buf) throws WlmrException {
        id = new DataIdBody();
        id.parseBuf(buf);

        if (id.getDa().firstAddr() == 0) {
            throw new WlmrException("invalid terminal address: 0");
        }

        short len = (short) (getGuessedLength() - 4);

        // same DA.
        List<WlmrBaseUnit> bodys = new ArrayList<>();
        unitList.add(bodys);

        for (short da : id.getDa().addrList()) {
            // DT list for one DA.
            for (short dt : id.getDt().typeList()) {

                DataIdBody outerId = new DataIdBody();
                outerId.getDa().setAddrList(da);
                outerId.getDt().setTypeList(dt);

                switch (dt) {
                case F_CURVE:

                {
                    AfnSecondF401Unit body = new AfnSecondF401Unit(len);
                    body.setOuterId(outerId);

                    int plen = body.parseBuf(buf);
                    if (plen == -1) {
                        throw new WlmrException("not enough bytes for 0D-F401.");
                    }

                    len -= plen;
                    bodys.add(body);

                    break;
                }
                case F_FROZEN_DAY:

                {
                    AfnSecondF409Unit body = new AfnSecondF409Unit(len);
                    body.setOuterId(outerId);

                    int plen = body.parseBuf(buf);
                    if (plen == -1) {
                        throw new WlmrException("not enough bytes for 0D-F409.");
                    }

                    len -= plen;
                    bodys.add(body);

                    break;
                }
                case F_FROZEN_MONTH:

                {
                    AfnSecondF410Unit body = new AfnSecondF410Unit(len);
                    body.setOuterId(outerId);

                    int plen = body.parseBuf(buf);
                    if (plen == -1) {
                        throw new WlmrException("not enough bytes for 0D.F410.");
                    }

                    len -= plen;
                    bodys.add(body);

                    break;
                }

                default:

                {
                    WlmrBaseUnit body = new WlmrDefaultUnit(len);
                    body.setOuterId(outerId);

                    len -= body.parseBuf(buf);
                    bodys.add(body);

                    break;
                }
                }
            }
        }

        return getGuessedLength() - len;
    }
}
